package com.benbi.shequapi.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.benbi.shequapi.bean.Community;
import com.benbi.shequapi.mapper.BuildinMapper;
import com.benbi.shequapi.mapper.BuildingMapper;
import com.benbi.shequapi.mapper.CommunityMapper;
import com.benbi.shequapi.mapper.LiveinMapper;
import com.benbi.shequapi.service.CommunityService;
import com.benbi.shequapi.utils.Result;
import com.benbi.shequapi.utils.ResultData;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service
public class CommunityImpl implements CommunityService {
    @Resource
    LiveinMapper liveinMapper;
    @Resource
    BuildingMapper buildingMapper;
    @Resource
    BuildinMapper buildinMapper;
    @Resource
    CommunityMapper communityMapper;

    @Override
    public Result getAllCommunity() {
        List<Community> list = communityMapper.selectAllCommunity();
        return new ResultData(list);
    }

    @Override
    public Result getCommunityByCid(int cid) {
        Community c = communityMapper.selectCommunityByCid(cid);
        if (c == null){
            return new Result(1, "社区不存在");
        }else {
            return new ResultData(c);
        }
    }

    @Override
    public Result getLiveCommunity(int pid) {
        ResultData liveResultData = ServiceUtil.getLiveDataByPid(liveinMapper, buildingMapper, buildinMapper, communityMapper, pid);
        if (!liveResultData.isSuccess()){return new Result(liveResultData);};

        JSONObject liveData = (JSONObject) liveResultData.getData();
        Community c = (Community) liveData.get("community");

        return new ResultData(c);
    }

}
